//
//  SpacingCell.m
//  s.
//
//  Created by fanshijian on 2022/8/24.
//  Copyright © 2022 J. All rights reserved.
//

#import "SpacingCell.h"
#import "SpaceModel.h"
#import "UIColor+YYAdd.h"
#import "NSString+YYAdd.h"

@implementation SpacingCell

static inline NSUInteger jy_hexStrToInt(NSString *str) {
    uint32_t result = 0;
    sscanf([str UTF8String], "%X", &result);
    return result;
}

static BOOL jy_HexStringToRGBA(NSString *str,
                         CGFloat *r, CGFloat *g, CGFloat *b, CGFloat *a) {
    str = [[str stringByTrim] uppercaseString];
    if ([str hasPrefix:@"#"]) {
        str = [str substringFromIndex:1];
    } else if ([str hasPrefix:@"0X"]) {
        str = [str substringFromIndex:2];
    }
    
    NSUInteger length = [str length];
    //         RGB            RGBA          RRGGBB        RRGGBBAA
    if (length != 3 && length != 4 && length != 6 && length != 8) {
        return NO;
    }
    
    //RGB,RGBA,RRGGBB,RRGGBBAA
    if (length < 5) {
        *r = jy_hexStrToInt([str substringWithRange:NSMakeRange(0, 1)]) / 255.0f;
        *g = jy_hexStrToInt([str substringWithRange:NSMakeRange(1, 1)]) / 255.0f;
        *b = jy_hexStrToInt([str substringWithRange:NSMakeRange(2, 1)]) / 255.0f;
        if (length == 4)  *a = jy_hexStrToInt([str substringWithRange:NSMakeRange(3, 1)]) / 255.0f;
        else *a = 1;
    } else {
        *r = jy_hexStrToInt([str substringWithRange:NSMakeRange(0, 2)]) / 255.0f;
        *g = jy_hexStrToInt([str substringWithRange:NSMakeRange(2, 2)]) / 255.0f;
        *b = jy_hexStrToInt([str substringWithRange:NSMakeRange(4, 2)]) / 255.0f;
        if (length == 8) *a = jy_hexStrToInt([str substringWithRange:NSMakeRange(6, 2)]) / 255.0f;
        else *a = 1;
    }
    return YES;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.whiteColor;
    }
    return self;
}

- (void)setModel:(SpaceModel *)model {
    if (self.model != model) {
        [super setModel:model];
        if (model.color) {
            // qmui color支持 #AARRGGBB
            self.backgroundColor = KLColor(model.color);
        }else {
            self.backgroundColor = [UIColor clearColor];
        }
    }
}

@end
